<?php

/**
 * winbinder 패키지 
 * 
 * 
 * @package php.ui.winbinder
 */


/**
 * WBPanel 
 *
 * @package php.ui.winbinder
 */
class WBPanel extends WBContainer { 
	private $_width = 0;
	private $_height = 0;
	private $_x = 0;
	private $_y = 0;
	private $_visible = true;

	public function __construct($options = array()) { 
		parent::__construct($options);
	}

	public function __destruct() { 
		parent::__destruct();
		
		unset($this->_width);
		unset($this->_height);
		unset($this->_x);
		unset($this->_y);
	}		
	
	public function initComponent() { 
		parent::initComponent();

		if (isset($this->margin)) {
			$margin = explode(" ", (string)$this->getOption('margin'));
			$size = count($margin);
			
			if ($size == 1) { 
				$this->setOption('marginLeft', $margin[0]);
				$this->setOption('marginTop', $margin[0]);
				$this->setOption('marginRight', $margin[0]);
				$this->setOption('marginBottom', $margin[0]);
			} else if ($size == 2) { 
				$this->setOption('marginLeft', $margin[0]);
				$this->setOption('marginTop', $margin[1]);
				$this->setOption('marginRight', $margin[0]);
				$this->setOption('marginBottom', $margin[1]);				
			} else if ($size == 3) { 
				$this->setOption('marginLeft', $margin[0]);
				$this->setOption('marginTop', $margin[1]);
				$this->setOption('marginRight', $margin[2]);
				$this->setOption('marginBottom', $margin[1]);				
			} else if ($size == 4) { 
				$this->setOption('marginLeft', $margin[0]);
				$this->setOption('marginTop', $margin[1]);
				$this->setOption('marginRight', $margin[2]);
				$this->setOption('marginBottom', $margin[3]);				
			}
		}
		
		$this->setOption('marginLeft', (isset($this->marginLeft)	?	$this->getOption('marginLeft'): 0));
		$this->setOption('marginRight', (isset($this->marginRight)	? $this->getOption('marginRight')	: 0));
		$this->setOption('marginTop', (isset($this->marginTop)		? $this->getOption('marginTop')		: 0));
		$this->setOption('marginBottom', (isset($this->marginBottom)	? $this->getOption('marginBottom')	: 0));

		// 기본 여백 설정 
		$this->setMarginAll(array(
			'left'	=> $this->getOption('marginLeft'),	
			'right' => $this->getOption('marginRight'),	
			'top'	=> $this->getOption('marginTop'),	
			'bottom' => $this->getOption('marginBottom') 	
		));

		if(isset($this->layout)) $this->setLayout($this->getOption('layout'));
		if(isset($this->visible)) $this->setVisible($this->getOption('visible'));
		
		
		if (!isset($this->autoVisible)) $this->setOption('autoVisible', true);		
		
		if (isset($this->controls)) { 
			foreach ($this->getOption('controls') as $control) { 
				$this->add($control);
			}
		}
		
		// set real bounds 
		$this->initBounds();
	}
	
	public function initBounds() { 
		$this->_x = $this->getOption('x');
		$this->_y = $this->getOption('y');
		$this->_width = $this->getOption('width');
		$this->_height = $this->getOption('height');
	}

	/**
	 * 위치 지정 
	 * 
	 * @see WBComponent#setPosition($x, $y)
	 */
	public function setPosition($x = 0, $y = 0) { 
		//$this->setOption('x', $x);
		//$this->setOption('y', $y);
		
		$this->_x = $x;
		$this->_y = $y;		
	}

	/**
	 * 위치 구하기 
	 * 
	 * @see WBComponent#getPosition($clientarea)
	 */
	public function getPosition($lparam = false) { 
		if ($lparam) { 
			return array(
				'x' => $this->_x + $this->getMargin('left'), 
				'y' => $this->_y + $this->getMargin('top')
			);
		} else { 
			return array(
				'x' => $this->_x, 
				'y' => $this->_y
			);
		}
	}

	/**
	 * 사이즈 지정
	 * 
	 * @see WBComponent#setSize($width, $height)
	 */
	public function setSize($width = 0, $height = 0) { 
		$this->_width = $width;
		$this->_height = $height;

		$this->fireEvent('resize');
	}

	/**
	 * 사이즈 구하기 
	 * 
	 * @see WBComponent#getSize($lparam)
	 */
	public function getSize($lparam = false) { 
		if ($lparam) { 
			return array(
				'width' => $this->_width - $this->getMargin('left') - $this->getMargin('right'), 
				'height' => $this->_height - $this->getMargin('top') - $this->getMargin('bottom')
			);
		} else { 
			return array(
				'width' => $this->_width, 
				'height' => $this->_height
			);
		}
	}

	/**
	 * panel 의 보이기 여부 설정 
	 * 
	 * 하위 컴포넌트들에 모두 전파한다. 
	 * 
	 * @see WBComponent#setVisible($isVisible)
	 */
	public function setVisible($isVisible = true) {
		$comList = $this->getComponentList(true);

		foreach ($comList as $com) { 
			
			if ($isVisible === false) { 
				$com->setVisible($isVisible);
			} else { 
				if ($com['autoVisible']) { 
					$com->setVisible($isVisible);
				}
			}	
		}
		
		$this->_visible = $isVisible;
		
		if ($isVisible) { 
			$this->doLayout();
		}
	}
	
	public function isVisible() { 
		return $this->_visible;
	}
	
	/**
	 * WBPanel 의 instance 를 얻어온다. 
	 * 
	 * panel 이 속해있는 window 의 instance 를 리턴한다. 
	 * 
	 * @see WBComponent#getInstance()
	 */
	public function getInstance() { 
		return WBApplication::getInstance(wbparent($this));
	}
	
	/**
	 * 하위 리스트들에 대해서 이벤트 전파 
	 * 
	 * @see WBComponent#onRedraw($eo)
	 */
	public function onRedraw($eo) {
		$this->repaint($this->getGraphics($eo));
		
		$comList = $this->getComponentList();
		
		foreach ($comList as $com) {
			if ($com instanceof WBPanel) { 
				$com->fireEvent('redraw', $eo);
			}
		}
	}	
	
	/**
	 * panel 영역만 다시 그림 
	 * 
	 * @see WBComponent#refresh($now)
	 */
	public function refresh() {
		$bounds = $this->getBounds(); 
		wbwindow($this->getInstance())->refresh(true, $bounds['x'], $bounds['y'], $bounds['width'], $bounds['height']);
	}
	
	/**
	 * Blank Panel 생성 
	 * 
	 * 여백으로 처리하기 힘든 공간을 주기 위해서 사용한다. 
	 * 
	 * @param array $options  width,height 속성 지정 
	 * @return WBPanel
	 */
	public function addBlank($options = array()) {

		$width = isset($options['width']) ? $options['width'] : 1; 
		$height = isset($options['height']) ? $options['height'] : 1; 
		
		return $this->add(array( 'width' => $width, 'height' => $height ));	
	}
	
	public function addLabel($caption = '', $height = 20) { 
		$temp = array('wbtype' => 'Label', 'caption' => $caption, 'height' => $height) ;
		
		return $this->add($temp);
	}
	
	/**
	 * Separator 생성 
	 * 
	 * WBFrame 클래스를 사용하여 Separator 를 생성한다.
	 * 가로 형태만 지원한다.  
	 * 
	 * @param int $width
	 * @return WBFrame
	 */
	public function addSeparator($width = 100, $caption = '') { 
		$this->add(array( 'wbtype' => 'Frame', 'width' => $width, 'caption' => $caption , 'height' => 9));
		return $this->addBlank();
	}	
}

?>